package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.enums.MchTypeEnum;
import com.sgzn.sgpay.model.request.*;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.PrivShopQueryResult;
import com.sgzn.sgpay.model.response.result.SubMchQueryResult;
import com.sgzn.sgpay.persistence.domain.TMerchant;
import com.sgzn.sgpay.service.SgsMerchantManageService;
import com.sgzn.sgpay.service.SgsMerchantQueryService;
import com.sgzn.sgpay.service.SgsPrivQueryService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 子商户管理控制器
 * @author zhongzhongyong
 * @version Id: SubMchController.java, v 0.1 2018/6/3 14:36 zhongzhongyong Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant/subMch")
public class SubMchController extends MerchantBaseController {

    @Resource
    private SgsMerchantManageService sgsMerchantManageService;

    @Resource
    private SgsMerchantQueryService sgsMerchantQueryService;

    @Resource
    private SgsPrivQueryService sgsPrivQueryService;

    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 新增子商户
     * @param request
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public SgsResultResponse<String>  add(SubMchAddRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantManageService.addSubMch(assembleSubMchAddRequest(request));
    }

    /**
     * 修改子商户
     * @param request
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public SgsResultResponse<String>  update(SubMchUpdateRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantManageService.updateSubMch(assembleSubMchUpdateRequest(request));
    }

    /**
     * 删除子商户
     * @param request
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public SgsResultResponse<String>  delete(SubMchDeteleRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantManageService.deleteSubMch(assembleSubMchDeleteRequest(request));
    }

    /**
     * 查询子商户详情信息
     * @param request
     * @return
     */
    @RequestMapping("queryDetail")
    @ResponseBody
    public SgsResultResponse<SubMchQueryResult> queryDetail(SubMchQueryDetailRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantQueryService.querySubMchInfo(assembleSubMchQueryDetailRequest(request));
    }

    /**
     * 子商户列表查询
     * @param request
     * @return
     */
    @RequestMapping("queryList")
    @ResponseBody
    public SgsResultResponse<List<TMerchant>> queryList(SubMchQueryListRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantQueryService.queryMchBaseList(assembleSubMchQueryListRequest(request));
    }

    /**
     * 权限列表和门店列表查询
     * @param request
     * @return
     */
    @RequestMapping("queryPrivAndShopList")
    @ResponseBody
    public SgsResultResponse<PrivShopQueryResult> queryPrivAndShopList(QueryPrivAndShopListRequest request) {
        super.setMchInfo(request);
        return this.sgsPrivQueryService.queryPrivAndShopList(assembleQueryPrivAndShopListRequest(request));
    }

    /**
     * 组装子商户新增请求
     * @param request
     * @return
     */
    private SgsSubMchAddRequest assembleSubMchAddRequest(SubMchAddRequest request) {
        SgsSubMchAddRequest target = new SgsSubMchAddRequest();
        this.assembleCommonHandle.assemble(request, target);
        target.setConfirmPwd(request.getConfirmPwd());
        target.setPassword(request.getPassword());
        target.setContact(request.getContact());
        target.setMerchantName(request.getMerchantName());
        target.setPrivIds(request.getPrivIds());
        target.setShopIds(request.getShopIds());
        return target;
    }

    /**
     * 组装子商户更新请求
     * @param request
     * @return
     */
    private SgsSubMchUpdateRequest assembleSubMchUpdateRequest(SubMchUpdateRequest request) {
        SgsSubMchUpdateRequest target = new SgsSubMchUpdateRequest();
        this.assembleCommonHandle.assemble(request, target);
        target.setShopIds(request.getShopIds());
        target.setPrivIds(request.getPrivIds());
        target.setSubMchId(request.getSubMchId());
        return target;
    }

    /**
     * 组装子商户删除请求
     * @param request
     * @return
     */
    private SgsSubMchDeleteRequest assembleSubMchDeleteRequest(SubMchDeteleRequest request) {
        SgsSubMchDeleteRequest target = new SgsSubMchDeleteRequest();
        this.assembleCommonHandle.assemble(request, target);
        target.setSubMchId(request.getSubMchId());
        return target;
    }

    /**
     * 组装子商户详情查询请求
     * @param request
     * @return
     */
    private SgsSubMchQueryRequest assembleSubMchQueryDetailRequest(SubMchQueryDetailRequest request) {
        SgsSubMchQueryRequest target = new SgsSubMchQueryRequest();
        this.assembleCommonHandle.assemble(request, target);
        target.setSubMchId(request.getSubMchId());
        return target;
    }


    /**
     * 组装子商户列表查询请求
     * @param request
     * @return
     */
    private SgsMchQueryListRequest assembleSubMchQueryListRequest(SubMchQueryListRequest request) {
        SgsMchQueryListRequest target = new SgsMchQueryListRequest();
        this.assembleCommonHandle.assemble(request, target);
        target.setMchType(MchTypeEnum.SUB_MERCHANT.getCode());
        return target;
    }

    /**
     * 组装权限列表和门店列表查询请求
     * @param request
     * @return
     */
    private SgsQueryPrivAndShopRequest assembleQueryPrivAndShopListRequest(QueryPrivAndShopListRequest request) {
        SgsQueryPrivAndShopRequest target = new SgsQueryPrivAndShopRequest();
        this.assembleCommonHandle.assemble(request, target);
        return target;
    }


}

